package com.aggregate.pay.payeco.payment.test.client;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aggregate.pay.payeco.payment.PayecoHttpClient;


public class ExampleRequestClient implements PayecoHttpClient {

	private static Logger log = LoggerFactory.getLogger(ExampleRequestClient.class);
	
	@Override
	public String post(String server, String requestBody) {
		HttpUriRequest httpUriRequest = RequestBuilder
				.post()
				.setUri(server)
				.setEntity(new StringEntity(requestBody, "UTF-8"))
				.build();
		CloseableHttpClient httpClient = HttpClientFactory.createHttpClient();
		CloseableHttpResponse response;
		try {
			response = httpClient.execute(httpUriRequest);
			int status = response.getStatusLine().getStatusCode();
			log.debug("远程接口返回状态：{}", response.getStatusLine());
	        if (status == 200) {
				return EntityUtils.toString(response.getEntity(), "UTF-8");
	        } 
		} catch (Exception e) {
			log.error("远程接口调用失败", e);
		} 
		return null;
	}
}
